package com.taijihuabao.common.activititasklistener.quittasklistener;

import com.taijihuabao.modules.entity.ApprovalEntity;
import com.taijihuabao.modules.entity.ApprovalHistoryEntity;
import com.taijihuabao.modules.mapper.ApprovalHistoryMapper;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.task.IdentityLink;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author wang
 */
public class AddHistoryUtil {

    public static ApprovalHistoryEntity addHistory(DelegateTask delegateTask, ApprovalEntity approvalEntity, String deptCode) {

        Set<IdentityLink> users = delegateTask.getCandidates();
        List<String> userIds = new ArrayList<>();
        for (IdentityLink user : users) {
            userIds.add(user.getUserId());
        }

        //新建任务时添加节点历史数据
        ApprovalHistoryEntity approvalHistoryEntity = new ApprovalHistoryEntity();
        approvalHistoryEntity.setApprovalType(approvalEntity.getApprovalType());
        approvalHistoryEntity.setApprovalCode(approvalEntity.getApprovalCode());
        approvalHistoryEntity.setApplyUserId(approvalEntity.getApplyUserId());
        approvalHistoryEntity.setApplyDeptCode(approvalEntity.getApplyDeptCode());
        approvalHistoryEntity.setApplyName(approvalEntity.getApplyName());
        approvalHistoryEntity.setApprovalUserId(Long.valueOf(userIds.get(0)));
        approvalHistoryEntity.setApprovalDeptCode(deptCode);
        approvalHistoryEntity.setApprovalState("ing");
        approvalHistoryEntity.setApprovalTask(delegateTask.getId());
        approvalHistoryEntity.setApprovalFlow(delegateTask.getName());

        return approvalHistoryEntity;
    }
}
